package cn.andrewlu.weexshell.ui.nav;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieComposition;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;

import java.util.ArrayList;
import java.util.List;

import cn.andrewlu.weexshell.R;
import cn.andrewlu.weexshell.beans.BottomNav;
import cn.andrewlu.weexshell.beans.NavData;
import cn.andrewlu.weexshell.beans.NavItem;
import cn.andrewlu.weexshell.ui.NavFragment;
import cn.andrewlu.weexshell.ui.WXPageFragment;
import cn.andrewlu.weexshell.utils.AssetsTool;
import cn.andrewlu.weexshell.utils.ViewIdGenerator;

/**
 * Created by andrewlu on 2017/6/4.
 * 底部导航类型
 * 包含底部导航栏和标题栏 以及 若干SinglePageFragment.
 * 标题栏先固定所有页面共享.SinglePageFragment此时就选择不带标题栏,其标题管理由BottomNavFragment进行接管.
 * 对外提供方法:
 * openDetail()
 * 提供标题栏控制方法.
 * <p>
 * 底部导航数据结构:
 * {
 * type:BOTTOM_NAV,
 * titlebar:{
 * <p>
 * },
 * navbar:{
 * <p>
 * },
 * pages:{
 * <p>
 * }
 * }
 */

public class BottomNavFragment extends NavFragment {
    private BottomNavigationBar mNavBar;
    private ViewPager mContentView;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        LinearLayout root = new LinearLayout(getContext());
        root.setOrientation(LinearLayout.VERTICAL);

        mContentView = new ViewPager(getContext());
        mContentView.setId(ViewIdGenerator.id());
        LinearLayout.LayoutParams contentLp = new LinearLayout.LayoutParams(-1, 0);
        contentLp.weight = 1;
        root.addView(mContentView, contentLp);

        mNavBar = new BottomNavigationBar(getContext());
        mNavBar.setId(ViewIdGenerator.id());
        LinearLayout.LayoutParams navLp = new LinearLayout.LayoutParams(-1, -2);
        root.addView(mNavBar, navLp);
        ((FrameLayout) view).addView(root);

        initNavBar(mNavBar);
        initContentView(mContentView);
    }

    private void initNavBar(BottomNavigationBar bar) {
        BottomNav navData = (BottomNav) getNavData();

        bar.setMode(BottomNavigationBar.MODE_FIXED);
        bar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        for (NavItem item : navData.navs) {
            Drawable icon = AssetsTool.getDrawable(getContext(), item.iconRes);
            BottomNavigationItem navItem = new BottomNavigationItem(icon, item.text);
            navItem.setActiveColor(item.activeColor).setInActiveColor(item.inActiveColor).animate(false);
            bar.addItem(navItem);
        }
        bar.setFirstSelectedPosition(0).initialise();
        bar.setTabSelectedListener(new BottomNavigationBar.SimpleOnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                mContentView.setCurrentItem(position, true);
            }
        });
    }

    private void initContentView(ViewPager contentRoot) {
        BottomNav navData = (BottomNav) getNavData();
        final List<Fragment> pages = new ArrayList<>(navData.pages.size());
        for (String page : navData.pages) {
            pages.add(new WXPageFragment(page));
        }
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return pages.get(position);
            }

            @Override
            public int getCount() {
                return pages.size();
            }
        };
        contentRoot.setAdapter(adapter);
        contentRoot.setOffscreenPageLimit(pages.size());
        contentRoot.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mNavBar.selectTab(position, false);
            }
        });
    }

}
